package co.edu.unal.satacredito.rest.resources.visa;


import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;

import co.edu.unal.satacredito.rest.activities.visa.ValidateDebitCardTransactionActivity;
import co.edu.unal.satacredito.rest.domain.visa.DebitCardTransaction;
import co.edu.unal.satacredito.rest.exceptions.visa.NoSuchTransactionException;
import co.edu.unal.satacredito.rest.representations.Representation;
import co.edu.unal.satacredito.rest.representations.RestUri;
import co.edu.unal.satacredito.rest.representations.visa.DebitCardTransactionRepresentation;

@Path("/validation/visa/debitCard/")
public class DebitCardTransactionResource {

	private @Context
	UriInfo uriInfo;

	public DebitCardTransactionResource() {
	}

	@POST
	@Consumes(Representation.SATACREDITO_MEDIA_TYPE)
	@Produces(Representation.SATACREDITO_MEDIA_TYPE)
	public Response validateTransaction(DebitCardTransaction debitCardTransaction) {
		try {
			DebitCardTransactionRepresentation responseRepresentation = new ValidateDebitCardTransactionActivity()
					.validate(
							debitCardTransaction,
							new RestUri(uriInfo.getRequestUri()));
			return Response.ok().entity(responseRepresentation).build();
		} catch (NoSuchTransactionException nsoe) {
			nsoe.printStackTrace();
			return Response.status(Status.NOT_FOUND).build();
		} catch (Exception ex) {
			ex.printStackTrace();
			return Response.serverError().build();
		}
	}
}
